﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UseCaseMVVMValidador.Test.Validacoes
{
    [TestClass]
    public class Asynchronous
    {
        [TestMethod]
        public void Testar()
        {
            var listing01 = new Listing_01();
            listing01.Executar();
        }



        [TestMethod]
        public void TEstar2()
        {
            // create the cancellation token source
            var tokenSource = new CancellationTokenSource();
            // create the cancellation token
            var token = tokenSource.Token;
            // create the task
            var task1 = new Task(() =>
            {
                for (int i = 0; i < int.MaxValue; i++)
                {
                    if (token.IsCancellationRequested)
                    {
                        Console.WriteLine("Task cancel detected");
                        throw new OperationCanceledException(token);
                    }
                    else
                    {
                        Console.WriteLine("Int value {0}", i);
                    }
                }
            }, token);


            // create a second task that will use the wait handle
            var task2 = new Task(() =>
                                      {
                                          // wait on the handle
                                          token.WaitHandle.WaitOne();
                                          // write out a message
                                          Console.WriteLine(">>>>> Wait handle released");
                                      });

            // wait for input before we start the task
            Console.WriteLine("Press enter to start task");
            Console.WriteLine("Press enter again to cancel task");
            Console.ReadLine();


            // start the tasks
            task1.Start();
            task2.Start();

            // read a line from the console.
            Console.ReadLine();

            // cancel the task
            Console.WriteLine("Cancelling task");
            tokenSource.Cancel();

            // wait for input before exiting
            Console.WriteLine("Main method complete. Press enter to finish.");
            Console.ReadLine();            
        }




        [TestMethod]
        public void Tarefa_com_tempo_de_espera()
        {
            var tokenSource = new CancellationTokenSource();
            var token = tokenSource.Token;

            var task1 = new Task(() =>
                                     {
                                         for (int i = 0; i < Int32.MaxValue; i++)
                                         {
                                             var cancelled = token.WaitHandle.WaitOne(10000);
                                             Console.WriteLine("Task 1 - Int value {0}. Cancelled? {1}", i, cancelled);

                                             if (cancelled)
                                                 throw new OperationCanceledException(token);
                                         }
                                     }, token);
            task1.Start();

            Console.WriteLine("Press enter to cancel token.");
            Console.ReadLine();

            tokenSource.Cancel();

            Console.WriteLine("Main method complete. Press enter to finish.");
            Console.ReadLine();
        }
    }
}




class Listing_01
{
    public void Executar()
    {
        Task.Factory.StartNew(() =>
                                  {
                                      Debug.WriteLine("******************************************************** Hello World");
                                  });

        // wait for input before exiting
        Debug.WriteLine("********************************************** Main method complete. Press enter to finish.");

        
    }
}
